package com.example.batterynotification;

import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceActivity;
import android.preference.SwitchPreference;
import android.util.Log;
import android.widget.Toast;

public class MainSettingsActivity extends PreferenceActivity {

	private Intent mIntent;
	private AppContext appContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.layout.main_setting_layout);
		appContext = new AppContext(this);
		new Preferences(appContext);
		new ManageNotifications(appContext);
		// .............................................................................
		@SuppressWarnings("deprecation")
		final CustomSwitchPrefrence highPref = (CustomSwitchPrefrence) findPreference("HighSwitch");
		highPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

			@Override
			public boolean onPreferenceClick(Preference preference) {
				startActivity(highPref.getIntent());
				return true;
			}
		});

		highPref.setOnCustomSwitchChangeListener(new OncustomSwitchChangeListener() {

			@Override
			public void onEvent(Boolean newState) {
				mIntent = new Intent(getApplicationContext(),
						ListeningService.class);

				if (newState || (Preferences.getBoolPreferences("LowSwitch"))) {
					startTheServiece(mIntent);
				} else {
					stopTheServiece(mIntent);
				}
			}
		});

		@SuppressWarnings("deprecation")
		final CustomSwitchPrefrence lowPref = (CustomSwitchPrefrence) findPreference("LowSwitch");
		lowPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {

			@Override
			public boolean onPreferenceClick(Preference preference) {
				startActivity(lowPref.getIntent());
				return true;
			}
		});
		lowPref.setOnCustomSwitchChangeListener(new OncustomSwitchChangeListener() {

			@Override
			public void onEvent(Boolean newState) {
				mIntent = new Intent(getApplicationContext(),
						ListeningService.class);
				if (newState || (Preferences.getBoolPreferences("HighSwitch"))) {
					startTheServiece(mIntent);
				} else {
					stopTheServiece(mIntent);
				}

			}
		});
		// .........................................................................
		@SuppressWarnings("deprecation")
		final Preference AboutPref = (Preference) findPreference("AboutBattery");
		AboutPref
				.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
					@Override
					public boolean onPreferenceClick(Preference preference) {
						Intent intent = new Intent(getApplicationContext(),
								AboutBatteryActivity.class);
						Log.i("LC", "before start");
						startActivity(intent);
						Log.i("LC", "Start About Fragment");
						return true;
					}
				});

	}

	private void startTheServiece(Intent intent) {
		try {
			if (!Preferences.getBoolPreferences("isServiceStarted")) {
				startService(intent);
			}
		} catch (Exception ex) {

		}
	}

	private void stopTheServiece(Intent intent) {
		try {
			if (Preferences.getBoolPreferences("isServiceStarted")) {
				stopService(intent);
			}
		} catch (Exception ex) {

		}
	}

}
